package com.zlt.controller.recruiter;

import com.zlt.controller.BaseController;
import com.zlt.dto.InterviewDTO;
import com.zlt.service.InterviewService;
import com.zlt.service.impl.InterviewServiceImpl;
import com.zlt.util.ClassUtil;
import com.zlt.vo.InterviewVo;
import com.zlt.vo.Pager;
import com.zlt.vo.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/recruiter/interview/*")
public class InterviewController extends BaseController {

    InterviewService interviewService = new InterviewServiceImpl();

    @Override
    protected void defaultMethod(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    /**
     * 发起面试
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result addInterview(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        InterviewDTO interviewDTO = ClassUtil.resolveRequest(InterviewDTO.class, request);
        interviewDTO.setState(0);
        try {
            boolean success = interviewService.publishInterview(interviewDTO);
            return success ? Result.ok("发起成功") : Result.error("发起失败");
        } catch (RuntimeException e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 查询全部面试
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result selectAllInterview(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        InterviewDTO interviewDTO = ClassUtil.resolveRequest(InterviewDTO.class, request);

        try {
            Pager<InterviewVo> pager = new Pager<>();
            pager.setPage(interviewDTO.getPage());
            pager.setLimit(interviewDTO.getLimit());

            pager = interviewService.selectAllInterview(pager,interviewDTO);
            return Result.ok("发布成功",pager);
        } catch (RuntimeException e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 修改面试状态
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result updateInterview(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.valueOf(request.getParameter("id"));
        String state = request.getParameter("state");
        System.out.println(id);
        System.out.println(state);
        try {
            boolean success = interviewService.updateInterview(id,state);
            return success ? Result.ok("发起成功") : Result.error("发起失败");
        } catch (RuntimeException e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }


}
